<template>
  <div class="my-applications-container">
    <el-card>
      <div slot="header">
        <span>我的申请</span>
      </div>
      
      <el-table :data="applications" style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="title" label="标题" show-overflow-tooltip />
        <el-table-column prop="processName" label="流程名称" width="150" />
        <el-table-column prop="status" label="状态" width="120">
          <template slot-scope="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="currentNode" label="当前节点" width="150" />
        <el-table-column prop="startTime" label="发起时间" width="180">
          <template slot-scope="scope">
            {{ formatDate(scope.row.startTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" @click="viewDetail(scope.row)">查看详情</el-button>
            <el-button
              v-if="scope.row.status === 'PENDING'"
              type="text"
              style="color: #F56C6C;"
              @click="cancelApproval(scope.row)"
            >
              撤销
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { getMyApplications, cancelInstance } from '@/api/approval'

export default {
  name: 'MyApplications',
  
  data() {
    return {
      applications: []
    }
  },
  
  created() {
    this.loadApplications()
  },
  
  methods: {
    loadApplications() {
      getMyApplications().then(res => {
        this.applications = res.data
      })
    },
    
    viewDetail(row) {
      this.$router.push(`/approval/detail/${row.id}`)
    },
    
    cancelApproval(row) {
      this.$confirm('确定要撤销该审批吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        cancelInstance(row.id).then(() => {
          this.$message.success('撤销成功')
          this.loadApplications()
        })
      }).catch(() => {})
    },
    
    getStatusType(status) {
      const map = {
        PENDING: 'warning',
        APPROVED: 'success',
        REJECTED: 'danger',
        CANCELLED: 'info'
      }
      return map[status] || 'info'
    },
    
    getStatusText(status) {
      const map = {
        PENDING: '审批中',
        APPROVED: '已通过',
        REJECTED: '已拒绝',
        CANCELLED: '已撤销'
      }
      return map[status] || status
    },
    
    formatDate(date) {
      if (!date) return ''
      const d = new Date(date)
      return d.toLocaleString('zh-CN')
    }
  }
}
</script>

<style scoped>
.my-applications-container {
  background: #fff;
  border-radius: 8px;
}
</style>

